package net.gooduo.spring_boot_template.config;

import io.swagger.annotations.ApiOperation;
import net.gooduo.spring_boot_template.util.SwaggerUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@SpringBootConfiguration
@EnableOpenApi
public class SwaggerConfig {
    // Boolean swaggerEnabled = true;
    @Value("${spring.profiles.active}")
    private String active;
    @Bean
    public Docket adminApi(Environment env) {
        return new Docket(DocumentationType.OAS_30)
                .enable(active.equals("dev"))
                .groupName("谷多 Sping boot 开发模板 后台接口")
                .apiInfo(new ApiInfoBuilder()
                        .title("谷多 Sping boot 开发模板 ")
                        .description("谷多 Sping boot 开发模板 ")
                        .version("v1.0.0")
                        .build())
                .securitySchemes(SwaggerUtil.getSecuritySchemes())
                .securityContexts(SwaggerUtil.getSecurityContexts())
//                .alternateTypeRules(SwaggerUtils.pageableTypeRule())
                .select()
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .apis(RequestHandlerSelectors.basePackage("com.gooduo.spring_boot_template.controller.admin").and(x -> x.isAnnotatedWith(ApiOperation.class)))
//                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.any())
                .build();
//                .securitySchemes(SwaggerUtils.getSecuritySchemes())
//                .securityContexts(SwaggerUtils.getSecurityContexts());
    }


    @Bean
    public Docket apiApi() {
        return new Docket(DocumentationType.OAS_30)
                .enable(active.equals("dev"))
                .groupName("谷多 Sping boot 开发模板 api接口")
                .apiInfo(new ApiInfoBuilder()
                        .title("谷多 Sping boot 开发模板 ")
                        .description("谷多 Sping boot 开发模板 ")
                        .version("v1.0.0")
                        .build())
                .securitySchemes(SwaggerUtil.getSecuritySchemes())
                .securityContexts(SwaggerUtil.getSecurityContexts())
//                .alternateTypeRules(SwaggerUtils.pageableTypeRule())
                .select()
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .apis(RequestHandlerSelectors.basePackage("com.gooduo.spring_boot_template.controller.api").and(x -> x.isAnnotatedWith(ApiOperation.class)))
//                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.any())
                .build();
//                .securitySchemes(SwaggerUtils.getSecuritySchemes())
//                .securityContexts(SwaggerUtils.getSecurityContexts());
    }
}
